﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNext.Domain;
using WebNext.UI.Utils;
using WebNext.UI.Properties;
using WebNext.UI.Utils.Validators;

namespace WebNext.UI.Models.Login
{
	public class ResetPasswordViewModel : MainViewModel
	{
		public string Email
		{
			get;
			private set;
		}

		public string CaptchaKey
		{
			get;
			set;
		}

		public string CaptchaValue
		{
			get;
			set;
		}

		public bool IsEmailValid
		{
			get;
			set;
		}

		public bool IsCaptchaValid
		{
			get;
			private set;
		}

		public ResetPasswordViewModel(string email) : base(false)
		{
			this.Email = email;
			this.IsEmailValid = true;
			this.IsCaptchaValid = true;

			this.ViewType = MainViewType.ResetPassword;

			this.PageTitle = string.Format(Resources.TitleFormat, RuntimeSettings.SitePrefix, Resources.ResettingPassword);
		}

		public ValidationResult Validate()
		{
			IValidator emailValidator = new EmailValidator(this.Email);
			ValidationResult result = emailValidator.Validate();
			if (!result.IsValid)
			{
				this.IsEmailValid = false;
			}

			IValidator captchaValidator = new CaptchaValidator(this.CaptchaKey, this.CaptchaValue);
			ValidationResult captchaValidationResult = captchaValidator.Validate();
			if (!captchaValidationResult.IsValid)
			{
				result.Merge(captchaValidationResult);
				this.IsCaptchaValid = false;
			}
			return result;
		}
	}
}